package cn.com.yang.thread;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import org.junit.Test;

/**
 * 单线程化的线程池
 * @author 杨亿金
 * @date 2022年8月26日-下午11:37:33
 * @since 1.0.0
 */
public class TestSingleForExcutors {
	public static final ExecutorService executorService = Executors.newSingleThreadExecutor();
	static {
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			
			@Override
			public void run() {
				// ... 
				try {
					executorService.shutdown();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}));
		
	}
	public static void main(String[] args) {
		LocalDate date = LocalDate.now();
//		ConcurrentMap
		//HashTable ta = new Hashtable<String,String>();
		//AudioClip 
		ThreadLocal local = new ThreadLocal<>();
//		Unsafe 
//		Lock lock =
//		ReentrantLock
//		CyclicBarrier
	}
	@Test
	public void test() {
		List<Map> list = new ArrayList<>();
		Map map1 = new HashMap();
		map1.put("name", "1");
		map1.put("type", "0");
		list.add(map1);
		Map map2 = new HashMap();
		map2.put("name", "2");
		map2.put("type", "0");
		list.add(map2);
		Map map3 = new HashMap();
		map3.put("name", "3");
		map3.put("type", "2");
		list.add(map3);
		Map map4 = new HashMap();
		map4.put("name", "4");
		map4.put("type", "2");
		list.add(map4);
		Map map5 = new HashMap();
		map5.put("name", "5");
		map5.put("type", "3");
		list.add(map5);
		Map map6 = new HashMap();
		map6.put("name", "6");
		map6.put("type", "3");
		list.add(map6);
		
		Map<String, List<Map<String, Object>>> group = list.stream().collect(Collectors.groupingBy(map->map.get("type").toString()));
		System.out.println(group.toString());
		List<Map<String,Object>> rlist = new ArrayList<>();
		for (String key:group.keySet()) {
			Map map_0 = new HashMap();
			map_0.put("values", group.get(key));
			map_0.put("type", key);
			
			rlist.add(map_0);
		}
		System.out.println(rlist.toString());
	}
}
